package www.lagou.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.junit.jupiter.api.Test;
import www.lagou.entity.Account;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;

public class AccountDao {

    public Account findByCard(String card) throws SQLException {

        //1.创建QueryRunner对象 自动模式,传入数据库连接池
        QueryRunner qr = new QueryRunner(DruidUtils.dataSource);

        //2.编写SQL
        String sql = "select * from account where card = ?";

        //3.执行update, 不需要传入连接对象
        Account account = qr.query(sql, new BeanHandler<Account>(Account.class), card);
        return account;
    }

    public int accountUpdate(Account account) throws SQLException {

        //1.创建QueryRunner对象 自动模式,传入数据库连接池
        QueryRunner qr = new QueryRunner(DruidUtils.dataSource);

        //2.编写SQL
        String sql = "update account set balance = ? where card = ?";

        //3.设置占位符参数
        Object[] param = {account.getBalance(),account.getCard()};

        //4.执行update, 不需要传入连接对象
        return qr.update(sql,param);
    }
}
